
import * as Blockly from 'blockly/core'
import Swal from 'sweetalert2'
import { COMMON_CODE_I_REG } from '@/utils/reg'
import { Notification } from 'element-ui'
var renameVar = function (name) {
  return name
}

Blockly.prompt = function (msg, defaultValue, callback) {
  Swal.fire({
    title: '变量',
    input: 'text',
    inputAttributes: {
      autocapitalize: 'off',
      maxlength: 28
    },
    showCancelButton: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    showLoaderOnConfirm: true,
    inputPlaceholder: '变量名最长为28位,不支持中文',
    allowOutsideClick: () => !Swal.isLoading(),
    inputValidator: function (value) {
      return new Promise(function (resolve, reject) {
        if (COMMON_CODE_I_REG.test(value)) {
          resolve()
        } else {
          Notification.error({
            title: '错误',
            customClass: 'create-variable-notification',
            message: `变量名最长28位，支持英文字母、数字和“_”（下划线），首字符必须为字母或“_”`
          })
          reject()
          Swal.enableButtons()
          Swal.enableInput()
        }
      })
    }
  }).then(result => {
    if (result.value) {
      callback(renameVar(result.value))
    }
  })
}
